
package com.bash.carrep.ui;

import com.bash.carrep.dao.enties.Login;
import com.bash.carrep.dao.general.CRUD;
import com.bash.carrep.ui.datasource.UserContainer;
import com.bash.carrep.ui.util.UserFieldFactory;
import com.vaadin.ui.Window;
import java.util.Arrays;

/**
 *
 * @author p.bashizi
 */
public class UserForm extends AbstractFrom {

    Login login;

    public UserForm(CarrepApplication app) {

        super(app, Login.class);
        setFormFieldFactory(new UserFieldFactory());
        login = (Login) pojo;

        setItemDataSource(bi);
        // getItemDataSource().removeItemProperty("loginList");
        setVisibleLabels(Arrays.asList(new String[]{"username", "password", "creditLevel", "actif"}));

        buildButtons();
    }

    @Override
    void initDatasource() {
        form.setItemDataSource(bi);
    }

    @Override
    void buildButtons() {
        standardfooter = new EditSaveCancelView<Login>(new UserContainer(), this);
        setFooter(standardfooter);
    }

    @Override
    void submit() {
        CRUD crud = CRUD.getInstance();

        try {
            Login data =(Login) pojo;
            crud.start();
            crud.persist(data);
            crud.stop();
            showNotification("Confirmation", Window.Notification.TYPE_HUMANIZED_MESSAGE,
                    Window.Notification.POSITION_CENTERED_TOP, "<b>Effectué avec succès !</b>");
            
            app.showCreditLevelView();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
